package com.suarte.webapp.action;

import com.suarte.core.Guarantee;

import java.io.Serializable;

import org.appfuse.service.GenericManager;

/**
 * Created by IntelliJ IDEA.
 * User: Gcastillo
 * Date: Mar 10, 2011
 * Time: 7:12:18 PM
 * To change this template use File | Settings | File Templates.
 */
public class GuaranteeForm extends BasePage implements Serializable {
    private GenericManager<Guarantee, Long> guaranteeManager;
    private Guarantee guarantee = new Guarantee();
    private Long id;

    public void setGuaranteeManager(GenericManager<Guarantee, Long> guaranteeManager) {
        this.guaranteeManager = guaranteeManager;
    }

    public Guarantee getGuarantee() {
        return guarantee;
    }

    public void setCurrency(Guarantee guarantee) {
        this.guarantee = guarantee;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        guaranteeManager.remove(guarantee.getId());
        addMessage("guarantee.deleted");

        return "list";
    }

    public String edit() {
        if (id != null) {
            guarantee = guaranteeManager.get(id);
        } else {
            guarantee = new Guarantee();
        }

        return "edit";
    }

    public String add() {
        guarantee = new Guarantee();

        return "add";
    }

    public String save() {
        boolean isNew = (guarantee.getId() == null);
        guaranteeManager.save(guarantee);

        String key = (isNew) ? "guarantee.added" : "guarantee.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
}
